/*********************************************************
@file

@author J. Di Mattina

@brief Deserialization functions for c-realize library.

This file has the implementation of the deserialize 
functionality for the c-realize library.
*********************************************************/
/* standard library includes */
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>

/* local includes */
#include "Crz.h"

/*********************************************************
 Static Functions
 *********************************************************/

/*********************************************************
 Public Functions
 *********************************************************/

/*********************************************************
 Crz_Deserialize
*//**
@brief Unpack a serialized data buffer.

The c-realize library uses the same data format string 
created for serialization as input to this function for 
unpacking messages described in the .cr files.

The end result is the struct described in the .h file being
fully populated with data from the serialized buffer.

@param [in]  buf    Buffer to store serialized data
@param [in]  fmt    Format string descibing the vararg
@param [out] ...    Vararg of items to pack

@return CRZ_SUCCESS on success,
        corrosponding error code on failure.
**********************************************************/
Crz_Result
Crz_Deserialize(char *buf, char *fmt, ...)
{
    Crz_Result result = CRZ_SUCCESS;
    Crz_Arg arg;
    va_list ap;

    /* check parameters */
    if (buf == NULL || fmt == NULL)
    {
        return CRZ_ERROR_INVALID_PARAM;
    }    

    /* set the arg list */
    va_start(ap, fmt);

    while (*fmt)
    {
        switch(*fmt)
        {
        case '1':
            arg.u8 = (uint8_t) va_arg(ap, uint32_t);
            /* already in host byte order */

            break;
        case '2':
            arg.u16 = (uint16_t) va_arg(ap, uint32_t);
            /* place in host byte order */
            break;
        case '4':
            arg.u32 = va_arg(ap, uint32_t);
            /* place in host byte order */
            break;
        case '8':
            arg.u64 = va_arg(ap, uint64_t);
            /* place in host byte order */
            break;
        case '*':
            /** @todo not today */
            break;
        case '{':
            /** @todo not today */
            break;
        default:
            break;
        }
        fmt++;
    }
    return result;
}
